package jp.sourceforge.akjrcp.ui.draw2d;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import jp.sourceforge.akjrcp.subtitle.StandardSubObject;
import jp.sourceforge.akjrcp.ui.draw2d.WaveFormFigureCanvas.CanvasMouse;
import jp.sourceforge.akjrcp.ui.draw2d.WaveFormFigureCanvas.TimeSelection;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.MouseEvent;
import org.eclipse.draw2d.MouseListener;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.TextLayout;
import org.eclipse.swt.widgets.Shell;

/*
 * show list of subtitle info
 */
public  class SubtitleListFigure extends TimeBasedFigure{
	public static Log log=LogFactory.getLog(SubtitleListFigure.class);
	
	List<StandardSubObject> standardSubObjectList=new ArrayList<StandardSubObject>();
        private Rectangle[] inRectangle;
        
        private int rectangleY=0;
        private int rectangleHeight=800;//height of rectangle
        
        //private Image[] labelImages=new Image[8];
        private Image labelImage;
        private String subObjectPath;
        private Shell shell;
        
       
        private RGB backgroundRGB=new RGB(200,200,200);
        private RGB controlRGB=new RGB(220,220,220);

        //for mover
        public static Rectangle[] startMoveRectangles=new Rectangle[0];//move controler
        public static Rectangle[] endMoveRectangles=new Rectangle[0];//move controler
        public static Rectangle[] bothMoveRectangles=new Rectangle[0];//move controler
        public static Rectangle[] betweenMoveRectangles=new Rectangle[0];//move controler
        int moveRectangleY2=35;
        int moveRectangleY=65;
        int moveRectangleY3=25;
        int moveRectangleHlafSize=8;
        int moveRectangleHlafSize2=12;
        int moveRectangleHlafSize3=6;
        
        /**
         * @param arg0
         * @param arg1
         */
        public SubtitleListFigure(Shell shell,int arg0, int arg1,double splitFrame) {
            super(arg0, arg1,splitFrame);
            this.shell=shell;
           
           
           setColor(backgroundRGB);
           

           
        }
        
        public void add(StandardSubObject sub){
        	standardSubObjectList.add(sub);
        	updateSubtitles();
        }
        
        public void sort(){
        	StandardSubObject subs[]=standardSubObjectList.toArray(new StandardSubObject[standardSubObjectList.size()]);
        	Arrays.sort(subs);
        	standardSubObjectList.clear();
        	for (int i = 0; i < subs.length; i++) {
        		standardSubObjectList.add(subs[i]);
			}
        }
        public void remove(StandardSubObject sub){
        	standardSubObjectList.remove(sub);
        	updateSubtitles();
        }
        
        public void updateSubtitles(){
        	sort();
            rerect();
            repaint();
        }
        
        public void setColor(RGB color){
            labelImage=AlphaUtils.createColorAlphaImageData(new Color(shell.getDisplay(), color),128);
            backgroundRGB=color;
        
        }
        
        public void setStandardSubObjectList(List<StandardSubObject> list){
            standardSubObjectList=list;
            inRectangle=null;//for rerect
           // rerect();
        }
        
        public StandardSubObject[] containSubtitles(int x,int y){
            if(standardSubObjectList!=null){
            List<StandardSubObject> tmp=new ArrayList<StandardSubObject>();
            for(int i=0;i<inRectangle.length;i++){
                if(inRectangle[i].contains(-getParent().getBounds().x+x,y)){
                    tmp.add(standardSubObjectList.get(i));
                }
                
            }
            return (StandardSubObject[]) tmp.toArray(new StandardSubObject[tmp.size()]);
            }else{
                return new StandardSubObject[0];
            }
        }
        
       public void updateScale(){
    	   rerect();
       }
        
       //TODO make multi get
       public StandardSubObject intersectItem(long time){
    	   StandardSubObject subs[]=(StandardSubObject[]) standardSubObjectList.toArray(new StandardSubObject[standardSubObjectList.size()]);
         
           for(int i=0;i<subs.length;i++){
        	   if(time>=subs[i].getStartTime() && time<=subs[i].getEndTime()){
        		   return subs[i];
        	   }
           }
           return null;
       }
       
       //TODO make multi get
       public StandardSubObject[] containSubtitles(TimeSelection selection){
    	   StandardSubObject subs[]=(StandardSubObject[]) standardSubObjectList.toArray(new StandardSubObject[standardSubObjectList.size()]);
          List list=new ArrayList();
           for(int i=0;i<subs.length;i++){
        	   if(selection.getStart()<=subs[i].getStartTime() && selection.getEnd()>=subs[i].getStartTime()){
        		   list.add(subs[i]);
        	   }else if(selection.getStart()<=subs[i].getEndTime() && selection.getEnd()>=subs[i].getEndTime()){
        		   list.add(subs[i]);
        	   }
           }
           return (StandardSubObject[]) list.toArray(new StandardSubObject[list.size()]);
       }
       
        /*
         * re calcurate rectangle when setScale or setSubtitle
         */
        public void rerect(){
            log.trace("rerect");
            if(standardSubObjectList!=null){
            StandardSubObject subs[]=(StandardSubObject[]) standardSubObjectList.toArray(new StandardSubObject[standardSubObjectList.size()]);
            Rectangle[] tmpRect=new Rectangle[subs.length];
            for(int i=0;i<subs.length;i++){
                int dx=calculateTimeToX(subs[i].getStartTime());
                int dx2=calculateTimeToX(subs[i].getEndTime());
                tmpRect[i]=new Rectangle(dx,rectangleY,dx2-dx,rectangleHeight);
                log.trace("subrect:"+tmpRect[i]);
            }
            
            inRectangle=tmpRect;
            
            if(inRectangle.length>0){
            //make mover
            betweenMoveRectangles=new Rectangle[inRectangle.length-1];
            startMoveRectangles=new Rectangle[inRectangle.length];
            endMoveRectangles=new Rectangle[inRectangle.length];
            bothMoveRectangles=new Rectangle[inRectangle.length];
            for(int i=0;i<inRectangle.length;i++){
            	startMoveRectangles[i]=new Rectangle(inRectangle[i].x,moveRectangleY,moveRectangleHlafSize*2,moveRectangleHlafSize*2);
            	endMoveRectangles[i]=new Rectangle(inRectangle[i].x+inRectangle[i].width-moveRectangleHlafSize*2,moveRectangleY,moveRectangleHlafSize*2,moveRectangleHlafSize*2);
            	bothMoveRectangles[i]=new Rectangle(inRectangle[i].x+inRectangle[i].width/2-moveRectangleHlafSize,moveRectangleY3,moveRectangleHlafSize3*2,moveRectangleHlafSize3*2);
                if(i!=inRectangle.length-1){
                	betweenMoveRectangles[i]=new Rectangle(inRectangle[i].x+inRectangle[i].width+(inRectangle[i+1].x - inRectangle[i].x-inRectangle[i].width)/2-moveRectangleHlafSize2,moveRectangleY2,moveRectangleHlafSize2*2,moveRectangleHlafSize2*2);
                    
                }
            }
            }
            
            }else{
                inRectangle=new Rectangle[0];
                betweenMoveRectangles=new Rectangle[0];
                startMoveRectangles=new Rectangle[0];
                endMoveRectangles=new Rectangle[0];
                bothMoveRectangles=new Rectangle[0];
            }
        }
        
        
        
        public void rerect(StandardSubObject target){
        	StandardSubObject subs[]=(StandardSubObject[]) standardSubObjectList.toArray(new StandardSubObject[standardSubObjectList.size()]);
            for (int i = 0; i < subs.length; i++) {
				if(subs[i]==target){
					int dx=calculateTimeToX(subs[i].getStartTime());
	                int dx2=calculateTimeToX(subs[i].getEndTime());
	                inRectangle[i]=new Rectangle(dx,rectangleY,dx2-dx,rectangleHeight);
				}
			}
        }
        
        public void paintFigure(Graphics g){
            if(inRectangle==null){
                rerect();
            }
            log.trace("srtlist-paint:"+this.getClientArea()+",inRectangle="+inRectangle.length);
            
            Color color=new Color(null,backgroundRGB);
            Rectangle rectangle=new Rectangle(labelImage.getBounds());
            
            TextLayout layout=new TextLayout(null);
            if(inRectangle.length>0){
                g.setForegroundColor(new Color(null,controlRGB));
                drawRectangles(g,startMoveRectangles);
                drawRectangles(g,endMoveRectangles);
                drawRectangles(g,betweenMoveRectangles);
                drawRectangles(g,bothMoveRectangles);
                }
            

            for(int i=0;i<inRectangle.length;i++){
                //g.drawRectangle(inRectangle[i].x+getParent().getBounds().x,inRectangle[i].y,inRectangle[i].width,inRectangle[i].height);//TODO image & label.
                Rectangle drawRec=new Rectangle(inRectangle[i].x+getParent().getBounds().x,inRectangle[i].y,inRectangle[i].width,inRectangle[i].height);
                
                StandardSubObject sub=((StandardSubObject)standardSubObjectList.get(i));
                String option=sub.getOption();
                if(option!=null){
                    //option maybe index and ignore it
                }
               
               // g.fillRectangle(drawRec);
               
               if(drawRec.width>0){
            	   g.drawImage(labelImage,rectangle,new Rectangle(drawRec.x,drawRec.y,drawRec.width,128));//   
               }
                
               
                g.setForegroundColor(color);
                g.drawLine(drawRec.x,drawRec.y,drawRec.x,drawRec.y+drawRec.height);
                g.drawLine(drawRec.x+drawRec.width,drawRec.y,drawRec.x+drawRec.width,drawRec.y+drawRec.height);
                
                
                if(drawRec.width>10){//magic number have a space
                
                int marginH=25;
                int marginW=4;
                //TextLayout layout=new TextLayout(null);
                //layout.setAlignment(SWT.CENTER);
                layout.setWidth(drawRec.width-marginW*2);
                layout.setText(sub.getText());
                //g.clipRect(drawRec);
                g.setForegroundColor(ColorConstants.black);
                g.drawTextLayout(layout, drawRec.x+marginW,drawRec.y+marginH);
                }
                
                
            }
            


            layout.dispose();//text layout make a memory problem
        }
        
        public void drawRectangles(Graphics g,Rectangle[] rects){
        	Rectangle rectangle=new Rectangle(labelImage.getBounds());
            for(int i=0;i<rects.length;i++){
            	if(rects[i]!=null){
                //g.drawRectangle(inRectangle[i].x+getParent().getBounds().x,inRectangle[i].y,inRectangle[i].width,inRectangle[i].height);//TODO image & label.
                Rectangle fillRec=new Rectangle(rects[i].x+getParent().getBounds().x,rects[i].y,rects[i].width,rects[i].height);
                if(fillRec.width>0){
             	   g.drawImage(labelImage,rectangle,new Rectangle(fillRec.x,fillRec.y,fillRec.width,fillRec.height));//   
                }
                g.drawRectangle(fillRec);
            	}else{
            		log.warn("moverRectangles is null");
            	}
            }
        }
        
        
        public int getRectangleHeight() {
            return rectangleHeight;
        }
        public void setRectangleHeight(int rectangleHeight) {
            this.rectangleHeight = rectangleHeight;
        }
        public int getRectangleY() {
            return rectangleY;
        }
        public void setRectangleY(int rectangleY) {
            this.rectangleY = rectangleY;
        }
        public List<StandardSubObject> getStandardSubObjectList() {
            return standardSubObjectList;
        }
        public String getSubObjectPath() {
            return subObjectPath;
        }
        public void setSubObjectPath(String subObjectPath) {
            this.subObjectPath = subObjectPath;
        }
    }